package com.t123.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class MyBatisPlusConfig implements MetaObjectHandler {
    /**
     *  增加的时候填充走这个方法
     * @param metaObject
     * */
    @Override
    public void  insertFill(MetaObject metaObject){
        //填充创建时间
        this.strictInsertFill(metaObject,"createTime", Date.class,new Date());
        //填充修改时间
        this.strictInsertFill(metaObject,"updateTime", Date.class,new Date());
    }

    /**
     *  修改的时候填充走这个方法
     * @param metaObject
     * */
    @Override
    public  void updateFill(MetaObject metaObject){
        this.strictUpdateFill(metaObject,"updateTime", Date.class,new Date());
    }

    //分页插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return  interceptor;
    }
}
